package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
*用户退出
*     *对于session进行销毁
*     *跳转回登录页面
 */
@WebServlet("/logoutServlet")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //让session失效
        /*
        *如果不用下面这行代码，点击完的退出后session还存在
        * 可以直接访问页面测试
         */
        System.out.println("session已销毁");
        //设置服务器保留时间为0，以便退出的时候，直接统计会话
        req.getSession().setMaxInactiveInterval(0);
        req.getSession().removeAttribute("usertemp");
       //清除会话同上
        req.getSession().invalidate();
        //跳转到登录
        //req.getRequestDispatcher("/login.jsp").forward(req, resp);
        resp.sendRedirect("login.jsp");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
